package com.bluetooth.phone;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetooth.phone.multi.BaseViewHolder;
import com.bluetooth.phone.multi.ItemTypeFactoryImp;
import com.bluetooth.phone.optionb.ItemDragHelper;
import com.hsae.bluetooth.phones.R;

import java.util.Collections;
import java.util.List;


public class SubAdapter extends BaseAdapter {

    private static final String TAG = "SubAdapter";
    private Context context;


    public SubAdapter(List<DockItem> dockItems, Context context) {
        super(dockItems);
        mTypeFactory = new ItemTypeFactoryImp();
        setSubSwapListener(this);
        this.context = context;
        mDragListener = new DragListener();
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        //接收方item设置dragListener
        holder.itemView.setOnDragListener(mDragListener);
        holder.itemView.setTag(position);
        holder.itemView.setId(R.id.item_view_layout);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                onItemDelete(holder.getBindingAdapterPosition(), mDialogSwapListener);
                notifyDataSetChanged();//增删时需要全量更新
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mDragViewHolder = holder;
                startDrag(holder, SubAdapter.this);
                //方案2
                //   mItemDragHelper.startDrag(holder);
                return false;
            }
        });

    }

    public void onItemSelect(RecyclerView.ViewHolder source) {
        //当拖拽选中时放大选中的view
        source.itemView.setScaleX(1f);
        source.itemView.setScaleY(1f);
    }

    public void onItemClear(RecyclerView.ViewHolder source) {
        //拖拽结束后恢复view的状态
        source.itemView.setScaleX(1.0f);
        source.itemView.setScaleY(1.0f);
    }

    @Override
    public void onItemInsert(DockItem dockItem) {
        Log.e(TAG, "insert position 0");
        contentList.add(0, dockItem);
        notifyItemInserted(0);
        notifyItemRangeChanged(0, contentList.size());
    }

    @Override
    public void onItemEndInsert(DockItem dockItem) {
        Log.e(TAG, "onItemEndInsert position ");
        contentList.add(contentList.size(), dockItem);
        notifyItemInserted(contentList.size());
        notifyItemRangeChanged(contentList.size(), 0);
    }

    @Override
    public void onItemMoved(int fromPosition, int toPosition ) {
        Log.e(TAG, "onItemMove fromPosition" + fromPosition);
        Log.e(TAG, "onItemMove toPosition" + toPosition);
        if (fromPosition < contentList.size() && toPosition < contentList.size()) {
            //交换数据位置
            Collections.swap(contentList, fromPosition, toPosition);
            //刷新位置交换
            notifyItemMoved(fromPosition, toPosition);
        }
    }
    private ItemDragHelper mItemDragHelper;
    public void attachedToDragHelper(ItemDragHelper dragHelper) {
        mItemDragHelper = dragHelper;
    }
}